package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.BaseApplication;
import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.adps.ScriptRoleAdp;
import com.baihan.angus.app.game.common.bean.response.RoomDetailResponse;
import com.baihan.angus.app.game.common.util.ImageLoader;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;
import com.zzhoujay.richtext.RichText;

public class ScriptIntroDialog extends Dialog {

    private Context mContext;
    private WindowManager.LayoutParams mLayoutParams;
    private TextView tvScriptName, tvNum, tvRole, tvTime, tvType, tvWriter, tvDescription, tvIntro;
    private QMUIRadiusImageView ivScriptPoster;
    private ImageView ivClose, ivExpand;
    private RecyclerView rvRole;

    private ScriptRoleAdp mScriptRoleAdp;

    private int isExpand = 0;

    public ScriptIntroDialog(@NonNull Context context) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        mLayoutParams.height = (int) (AppUtil.getScreenHeight(context) * 0.8);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_script_intro, (ViewGroup) null);
        setContentView(dialogView);
        ivClose = dialogView.findViewById(R.id.ivClose);
        ivExpand = dialogView.findViewById(R.id.ivExpand);
        ivScriptPoster = dialogView.findViewById(R.id.ivScriptPoster);
        rvRole = dialogView.findViewById(R.id.rvRole);
        tvScriptName = dialogView.findViewById(R.id.tvScriptName);
        tvNum = dialogView.findViewById(R.id.tvNum);
        tvRole = dialogView.findViewById(R.id.tvRole);
        tvTime = dialogView.findViewById(R.id.tvTime);
        tvType = dialogView.findViewById(R.id.tvType);
        tvWriter = dialogView.findViewById(R.id.tvWriter);
        tvDescription = dialogView.findViewById(R.id.tvDescription);
        tvIntro = dialogView.findViewById(R.id.tvIntro);

        mScriptRoleAdp = new ScriptRoleAdp();
        rvRole.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false));
        mScriptRoleAdp.bindToRecycleView(rvRole);

        ivClose.setOnClickListener(v -> {
            RichText.clear(this);
            cancel();
        });
    }

    public void initData(RoomDetailResponse.Data roomData) {

        if (isExpand == 0) {
            ivExpand.setImageResource(R.mipmap.ic_expand);
        } else {
            ivExpand.setImageResource(R.mipmap.ic_unexpand);
        }

        ivExpand.setOnClickListener(v -> {
            if (isExpand == 0) {
                tvIntro.setMaxLines(Integer.MAX_VALUE);
                tvIntro.setEllipsize(null);
                isExpand = 1;
                ivExpand.setImageResource(R.mipmap.ic_unexpand);
            } else {
                tvIntro.setMaxLines(2);
                tvIntro.setEllipsize(TextUtils.TruncateAt.END);
                isExpand = 0;
                ivExpand.setImageResource(R.mipmap.ic_expand);
            }
        });

        tvScriptName.setText(roomData.getName());
        tvNum.setText(roomData.getScript().getRole_num() + "人");
        tvRole.setText(roomData.getScript().getRole_sex_num());
        tvTime.setText(roomData.getScript().getScript_time() + "小时");
        tvWriter.setText(roomData.getScript().getAuthor());
        tvDescription.setText("“" + roomData.getScript().getDescription() + "”");
        tvIntro.setText(roomData.getScript().getIntro());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < roomData.getScript().getScript_type_text().size(); i++) {
            sb.append(roomData.getScript().getScript_type_text().get(i));
            sb.append(" ");
        }
        tvType.setText(sb.toString());

        ImageLoader.loadImage(BaseApplication.getApplication().getApplicationContext(),
                ivScriptPoster, roomData.getScript().getPoster(), R.mipmap.ic_test_bg, 0);

        mScriptRoleAdp.clear();
        mScriptRoleAdp.addAll(roomData.getScript_role());
    }
}
